package com.kexio.dynamic.config;

import com.kexio.dynamic.config.repository.ConfigRepository;
import com.kexio.dynamic.config.service.ConfigService;
import com.kexio.dynamic.config.service.DynamicConfigService;
import com.kexio.dynamic.config.service.impl.DefaultConfigService;
import com.kexio.dynamic.config.service.impl.DefaultDynamicConfigService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;

import jakarta.annotation.PostConstruct;

/**
 * 动态配置模块自动配置
 * 
 * 功能说明：
 * - 自动配置配置管理服务
 * - 自动配置动态配置服务
 * - 启用异步支持
 * 
 * @author Kexio
 * @since 1.0.0
 */
@Configuration
@EnableAsync
@ConditionalOnProperty(name = "kexio.dynamic.config.enabled", havingValue = "true", matchIfMissing = true)
public class DynamicConfigAutoConfiguration {

    private static final Logger log = LoggerFactory.getLogger(DynamicConfigAutoConfiguration.class);

    @PostConstruct
    public void init() {
        log.info("==> Kexio动态配置模块已启用");
        log.debug("动态配置模块包含: 配置管理、动态更新、监听器、注解支持");
    }

    /**
     * 配置管理服务
     */
    @Bean
    @ConditionalOnMissingBean
    public ConfigService configService(ConfigRepository configRepository, 
                                     org.springframework.core.env.Environment environment) {
        log.debug("==> 创建ConfigService实例");
        return new DefaultConfigService(configRepository, environment);
    }

    /**
     * 动态配置服务
     */
    @Bean
    @ConditionalOnMissingBean
    public DynamicConfigService dynamicConfigService(ConfigService configService) {
        log.debug("==> 创建DynamicConfigService实例");
        return new DefaultDynamicConfigService(configService);
    }
}
